/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainWindow.java
 *
 * Created on 04.11.2010, 08:53:39
 */
package hcc.GUI;

import hcc.comp.FiFi_Csv;
import hcc.app.*;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

/**
 *
 * @author Guybrush
 */
public class MainWindow extends JFrame implements TableModelListener {

    /** Creates new form MainWindow */
    public MainWindow(CORE app) {
        this.app = app;
        initComponents();
        initData();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fc = new javax.swing.JFileChooser();
        maintp = new javax.swing.JTabbedPane();
        lStatus = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        mb = new javax.swing.JMenuBar();
        mFile = new javax.swing.JMenu();
        miNew = new javax.swing.JMenuItem();
        mEdit = new javax.swing.JMenu();
        miUndo = new javax.swing.JMenuItem();
        miRedo = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();

        fc.setDialogType(javax.swing.JFileChooser.SAVE_DIALOG);
        fc.setFileFilter(new FiFi_Csv());
        fc.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("HC-Connect");

        maintp.setTabLayoutPolicy(javax.swing.JTabbedPane.SCROLL_TAB_LAYOUT);
        maintp.setTabPlacement(javax.swing.JTabbedPane.LEFT);
        maintp.setAutoscrolls(true);
        maintp.setInheritsPopupMenu(true);

        lStatus.setText("Datenbank '"+ app.adb +"' erfolgreich geladen...");

        jLabel1.setFont(new java.awt.Font("Lucida Console", 0, 18));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("HC-Media Database Administration Utility");
        jLabel1.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

        mFile.setText("File");

        miNew.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        miNew.setText("Neue Verbindung");
        mFile.add(miNew);

        mb.add(mFile);

        mEdit.setText("Edit");

        miUndo.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Z, java.awt.event.InputEvent.CTRL_MASK));
        miUndo.setText("Rückgängig");
        mEdit.add(miUndo);

        miRedo.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Y, java.awt.event.InputEvent.CTRL_MASK));
        miRedo.setText("Wiederholen");
        mEdit.add(miRedo);

        mb.add(mEdit);

        jMenu1.setText("CSV");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("Speichern");
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("Speichern unter...");
        jMenu1.add(jMenuItem2);

        mb.add(jMenu1);

        setJMenuBar(mb);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lStatus, javax.swing.GroupLayout.DEFAULT_SIZE, 607, Short.MAX_VALUE)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 607, Short.MAX_VALUE)
            .addComponent(maintp, javax.swing.GroupLayout.DEFAULT_SIZE, 607, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(maintp, javax.swing.GroupLayout.PREFERRED_SIZE, 277, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(lStatus))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFileChooser fc;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JLabel lStatus;
    private javax.swing.JMenu mEdit;
    private javax.swing.JMenu mFile;
    private javax.swing.JTabbedPane maintp;
    private javax.swing.JMenuBar mb;
    private javax.swing.JMenuItem miNew;
    private javax.swing.JMenuItem miRedo;
    private javax.swing.JMenuItem miUndo;
    // End of variables declaration//GEN-END:variables
    private CORE app;

    //TabbedPane ChangeListener
    ChangeListener cl = new ChangeListener() {
      public void stateChanged(ChangeEvent changeEvent) {
        JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent.getSource();
        app.tabFocus = sourceTabbedPane.getSelectedIndex();
        app.tabNameFocus = sourceTabbedPane.getTitleAt(app.tabFocus);
      }
    };
    // Fügt ins tabbedpane ein Scrollpane samt dem daten-table ein.
    private void initData() {
        maintp.addChangeListener(cl);
        for (int i = 0; i < app.tabNames.size(); i++) {
            app.tables.get(i).getModel().addTableModelListener(this);
            JScrollPane sp = new JScrollPane(app.tables.get(i));
            maintp.addTab(app.tabNames.get(i), sp);
            maintp.getComponent(i).addKeyListener(null);
        }
    }

    // Überprüft die tabellen auf änderungen
    public void tableChanged(TableModelEvent e) {
        int row = e.getFirstRow();
        int column = e.getColumn();
        TableModel model = (TableModel) e.getSource();
        app.colName = model.getColumnName(column);
        app.changeMade = (String) model.getValueAt(row, column);
        app.rowNumber = row;
        app.colNumber = column;
        //System.out.println(columnName);
        //System.out.println(odata);
        app.sql.update();
    }
}
